<template>
  <div id="specs_app">
    <div id="specs_control">
      <el-button type="danger" plain round @click="isShow.flag = true"
        >新增</el-button
      >
    </div>
    <el-table
      :data="specsList"
      style="width: 100%"
      default-expand-all
      row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column prop="specsname" label="规格名称"> </el-table-column>
      <el-table-column prop="attrs" label="规格参数">
        <template v-slot="hhh">
          <el-button
            type="success"
            size="mini"
            plain
            round
            v-for="(att, index) in hhh.row.attrs"
            :key="index"
            >{{ att }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="status" label="规格状态">
        <template v-slot="hhh">
          <el-button
            type="success"
            size="mini"
            plain
            round
            v-if="hhh.row.status == 1"
            >正常</el-button
          >
          <el-button type="danger" size="mini" plain round v-else
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template v-slot="$props">
          <el-button
            type="warning"
            size="mini"
            plain
            round
            @click="edit_specs($props.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            plain
            round
            @click="del_specs($props.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="count"
      :page-size="pagintor.size"
      @current-change='take_page'
    >
    </el-pagination>
    <Dialog :isShow="isShow" @reload='reload' ref="dialog_ref"/>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import Dialog from "../components/AddSpecs.vue";
export default {
  data() {
    return {
      pagintor: { page: 1, size: 1},
      isShow: { flag: false },
    };
  },
  components: {
    Dialog,
  },
  computed: {
    ...mapState("specs_model", ["specsList", "count"]),
  },
  methods: {
    del_specs(id){
      this.$axios.post('/specsdelete',{id}).then(res=>{
        if(res.code === 200){
          this.$message({
            type:'success',
            message:res.msg
          })
          this.reload()
        }
      })
    },
    edit_specs(obj){
      this.$refs.dialog_ref.sendData(obj)
      this.isShow.flag = true
    },
    take_page(page){
      this.pagintor.page = page
      this.specsListActions(this.pagintor);
      this.specsCountActions();
    },
    reload() {
      this.specsListActions(this.pagintor);
      this.specsCountActions();
    },
    ...mapActions("specs_model", ["specsListActions", "specsCountActions"]),
  },
  created() {
    this.specsListActions(this.pagintor);
    this.specsCountActions();
  },
};
</script>

<style>
#specs_control {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}
.el-table__header thead,
.el-table__header thead th {
  background-color: #ff674b;
  color: #fff;
  line-height: 40px;
  border: 0;
}

.el-pagination {
  margin-top: 20px;
}
</style>